<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class GiftBuyUsed extends Model
{
    public $table='gift_buy_used';

    public $timestamps= false;

    public $primaryKey='gift_buy_used_id';

    public $guarded = [];

    protected $orderno_perfix = 1001;

    public function getOrderNo()
    {
        $order_no = $this->orderno_perfix.time().str_pad(random_int(1,9999),6,0,STR_PAD_LEFT);

        if($this->where('order_no',$order_no)->first())
            $this->getOrderNo();

        return $order_no;
    }

    public function getGiftNo()
    {
        $gift_no = time().str_pad(random_int(1,9999),6,0,STR_PAD_LEFT);

        if($this->where('gift_no',$gift_no)->first())
            $this->getGiftNo();

        return $gift_no;
    }

    public function userBuy()
    {
        return $this->belongsTo(Userinfo::class,'id','buy_user_id');
    }

    public function userBind()
    {
        return $this->belongsTo(Userinfo::class,'id','use_user_id');
    }

    public function mgift()
    {
        return $this->hasOne(Mgift::class,'id_gift','id_merchant_gift')->select(['gift_bg','id_gift','gift_shading']);
    }
}
